home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Software Money Savers / VirtualDub / Source / VirtualDub-1.7.7-src.7z / src / disasm / source / encoder_tracedec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-11-05  |  2.8 KB  |  106 lines

  1. #include <vector>
  2. #include "ruleset.h"
  3.  
  4. #define iterate_forward(type, obj, it) if(0);else for(type::const_iterator it = (obj).begin(), it##End = (obj).end(); it != it##End; ++it)
  5.  
  6. void dump_tracedec(std::vector<char>& dst, const tRuleSystem& rulesys) {
  7.     long decomp_bytes = 0;
  8.     long packed_bytes = 0;
  9.  
  10.     dst.resize(72, 0);
  11.     dst.push_back(rulesys.size());
  12.  
  13.     iterate_forward(tRuleSystem, rulesys, it) {
  14.         const ruleset& rs = *it;
  15.         std::vector<std::pair<uint8, uint8> > last_match[4];
  16.         std::string last_result[4];
  17.  
  18.         iterate_forward(std::list<rule>, rs.rules, itRule) {
  19.             const rule& r = *itRule;
  20.             std::vector<char>::size_type l;
  21.             int prematch, postmatch;
  22.             int i, x, ibest;
  23.             
  24.             l = r.match_stream.size();
  25.  
  26.             ibest = 0;
  27.             prematch = postmatch = 0;
  28.  
  29.             for(i=0; i<4; ++i) {
  30.                 size_t l2 = last_match[i].size();
  31.                 if (l2 > l)
  32.                     l2 = l;
  33.                 int tprematch = std::mismatch(last_match[i].begin(), last_match[i].begin() + l2, r.match_stream.begin()).first - last_match[i].begin();
  34.                 int tpostmatch = std::mismatch(last_match[i].rbegin(), last_match[i].rbegin() + l2, r.match_stream.rbegin()).first - last_match[i].rbegin();
  35.  
  36.                 if (tprematch+tpostmatch > prematch+postmatch) {
  37.                     prematch = tprematch;
  38.                     postmatch = tpostmatch;
  39.                     ibest = i;
  40.                 }
  41.             }
  42.  
  43.             if (prematch > 7)
  44.                 prematch = 7;
  45.  
  46.             if (postmatch > 7)
  47.                 postmatch = 7;
  48.  
  49.             if (postmatch > l - prematch)
  50.                 postmatch = l - prematch;
  51.  
  52.             dst.push_back(ibest*64 + postmatch*8 + prematch);
  53.             dst.push_back(1+l - prematch - postmatch);
  54.  
  55.             for(x=prematch; x<l - postmatch; ++x) {
  56.                 dst.push_back(r.match_stream[x].first);
  57.                 dst.push_back(r.match_stream[x].second);
  58.             }
  59.  
  60.             decomp_bytes += l*2+1;
  61.  
  62.             std::rotate(last_match, last_match+3, last_match+4);
  63.             last_match[0] = r.match_stream;
  64.  
  65.             uint8 flags = 0;
  66.             if (r.is_66)        flags = 0x80;
  67.             else if (r.is_67)    flags = 0x81;
  68.             else if (r.is_f2)    flags = 0x82;
  69.             else if (r.is_f3)    flags = 0x83;
  70.             else {
  71.                 if (r.is_call)        flags |= 0x01;
  72.                 if (r.is_jcc)        flags |= 0x02;
  73.                 if (r.is_jump)        flags |= 0x04;
  74.                 if (r.is_return)    flags |= 0x08;
  75.                 if (r.is_invalid)    flags |= 0x10;
  76.                 if (r.is_imm8)        flags |= 0x20;
  77.                 if (r.is_imm16)        flags |= 0x40;
  78.                 if (r.is_imm32)        flags |= 0x60;
  79.             }
  80.  
  81.             dst.push_back(flags);
  82.             
  83.             ++decomp_bytes;
  84.         }
  85.  
  86.         dst.push_back(0);
  87.         dst.push_back(0);
  88.  
  89.         decomp_bytes += 2;
  90.     }
  91.  
  92. #ifndef _M_AMD64
  93.     static const char header[64]="[02|02] VirtualDub tracedec module (IA32:P4/Athlon V1.05)\r\n\x1A";
  94. #else
  95.     static const char header[64]="[02|02] VirtualDub tracedec module (AMD64:EM64T/Athlon64 V1.0)\r\n\x1A";
  96. #endif
  97.  
  98.     memcpy(&dst[0], header, 64);
  99.  
  100.     packed_bytes = dst.size() - 72;
  101.     memcpy(&dst[64], &packed_bytes, 4);
  102.  
  103.     decomp_bytes += (rulesys.size()+1)*sizeof(void *);
  104.     memcpy(&dst[68], &decomp_bytes, 4);
  105. }
  106.